package net.cyue.jpa;

import jakarta.persistence.SharedCacheMode;
import jakarta.persistence.ValidationMode;
import jakarta.persistence.spi.ClassTransformer;
import jakarta.persistence.spi.PersistenceUnitInfo;
import jakarta.persistence.spi.PersistenceUnitTransactionType;

import javax.sql.DataSource;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class CYuePersistenceUnitInfo implements PersistenceUnitInfo {

    private final String unitName;
    private Properties properties = new Properties();
    private final String persistenceProviderClassName;
    private List<String> managedClassNameList = new ArrayList<>();
    private final List<ClassTransformer> classTransformerList = new ArrayList<>();

    public CYuePersistenceUnitInfo(CYuePersistenceUnitInfoConfiguration configuration) {
        this.unitName = configuration.unitName;
        this.persistenceProviderClassName = configuration.persistenceProviderClassName;
        if (configuration.managedClassNameList != null) {
            this.managedClassNameList = configuration.managedClassNameList;
        }
        this.properties = configuration.getProperties();
    }

    @Override
    public String getPersistenceUnitName() {
        return this.unitName;
    }

    @Override
    public String getPersistenceProviderClassName() {
        return this.persistenceProviderClassName;
    }

    @Override
    public PersistenceUnitTransactionType getTransactionType() {
        return null;
    }

    @Override
    public DataSource getJtaDataSource() {
        return null;
    }

    @Override
    public DataSource getNonJtaDataSource() {
        return null;
    }

    @Override
    public List<String> getMappingFileNames() {
        return List.of();
    }

    @Override
    public List<URL> getJarFileUrls() {
        return List.of();
    }

    @Override
    public URL getPersistenceUnitRootUrl() {
        return null;
    }

    @Override
    public List<String> getManagedClassNames() {
        return this.managedClassNameList;
    }

    @Override
    public boolean excludeUnlistedClasses() {
        return false;
    }

    @Override
    public SharedCacheMode getSharedCacheMode() {
        return SharedCacheMode.NONE;
    }

    @Override
    public ValidationMode getValidationMode() {
        return ValidationMode.AUTO;
    }

    @Override
    public Properties getProperties() {
        return this.properties;
    }

    @Override
    public String getPersistenceXMLSchemaVersion() {
        return "3.0";
    }

    @Override
    public ClassLoader getClassLoader() {
        return this.getClass().getClassLoader();
    }

    @Override
    public void addTransformer(ClassTransformer classTransformer) {
        this.classTransformerList.add(classTransformer);
    }

    @Override
    public ClassLoader getNewTempClassLoader() {
        return this.getClass().getClassLoader();
    }
}

